package com.wx.chat.listener;


import com.wx.chat.model.ChatMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionConnectedEvent;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;

/**
 * @Author:LLH
 * @Date:2020/8/22
 * @Description:
 */
@Component
public class WebSocketEventListener {

    private static final Logger logger = LoggerFactory.getLogger ( WebSocketEventListener.class );

    @Autowired
    private SimpMessageSendingOperations messagingTemplate;

    @EventListener
    public void handleWebSocketConnectListener ( SessionConnectedEvent event ) {
        logger.info ( "Received a new web socket connection" );
    }

    @EventListener
    public void handleWebSocketDisconnectListener ( SessionDisconnectEvent event ) {
        StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap ( event.getMessage ( ) );

        String username = (String) headerAccessor.getSessionAttributes ( ).get ( "username" );
        if (username != null) {
            logger.info ( "用户" + username +"断开连接......");

            ChatMessage chatMessage = new ChatMessage ( );
            chatMessage.setType ( ChatMessage.MessageType.LEAVE );
            chatMessage.setSender ( username );

            messagingTemplate.convertAndSend ( "/topic/public",chatMessage );
        }
    }
}
